package com.example.money.test.Async;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.example.money.test.R;

import java.lang.ref.WeakReference;


public class TextAsyncTask extends Activity {
    private ImageView mImageView;
    private ProgressBar mProgressBar;
    private Button mButton;
    static String url = "http://www.swust.edu.cn/_upload/tpl/01/6a/362/template362/images/logo_1.png";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_layout);

        init();
        MyTask task = new MyTask();
        task.execute();

    }

    private void init() {
        mImageView = findViewById(R.id.img_mooc);
        mProgressBar = findViewById(R.id.pb_mypb);

    }

    class MyTask extends AsyncTask<Void,Integer,Void>{

        @Override
        protected Void doInBackground(Void... voids) {
            for(int i=0;i<100;i++){
                publishProgress(i);
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
            mProgressBar.setProgress(values[0]);
        }
    }

//    static class MyHandler extends Handler{
//        private final WeakReference<TextAsyncTask> mActivity;
//
//
//        MyHandler(TextAsyncTask mActivity) {
//            this.mActivity = new WeakReference<TextAsyncTask>(mActivity);
//        }
//
//        @Override
//        public void handleMessage(Message msg) {
//            super.handleMessage(msg);
//
//            TextAsyncTask a = mActivity.get();
//
//            if(a!=null){
//                a.mImageView.setImageBitmap((Bitmap) msg.obj);
//            }
//        }
//    }


}
